package com.heima.common.interceptor;



import com.heima.utils.common.UserThreadLocal;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class WmTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取header中的信息
        String userId = request.getHeader("userId");
        //把用户id存入ThreadLocal中
        if (userId != null) {
            UserThreadLocal.setUserId(Integer.valueOf(userId));
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //清理ThreadLocal
        UserThreadLocal.removeUserId();
    }
}
